import { isFalsityExcludeZero } from '@/utils/bizUtils';

const CHECK_UUID_TYPES = ['20', '33', '7'] // 20: 第二件数量, 33: 件装数量, 7: 数量

 // checkItemType：1-数字，2-文本
const CHECK_ITEM_TYPES = {
  Number: '1', // 数字
  Text: '2', // 文本
}

// 获取更改标准值要计算的上下差值字段下标
const getDiffValueIndex = (type, tableList, row) => tableList.findIndex(item =>
  item.attrCode == type &&
  item.skuNo == row.skuNo &&
  item.checkItemUuid == row.checkItemUuid
)

// 获取公式计算后的上下差值
const getComputeDiffValue = val => parseFloat((Number(val) * 0.05).toFixed(2))

// 获取正则替换后的标准值（保留两位小数）
const getRegReplaceVal = (val, precision = 2) => {
  const partten = new RegExp(`^(\\-)*(\\d+)\\.(\\d{${precision}}).*$`)
  let newVal = val.replace(/[^\d.]/g, '') // 清除“数字”和“.”以外的字符
  newVal = newVal.replace(/^\.{2,}/g, '.') // 把2个以上的.替换成一个
  newVal = newVal.replace(/^\./g, '') // 保证第一个字符为数字而不是.
  newVal = newVal.replace(/\.$/g, '') // 保证最后一个字符为数字而不是.
  newVal = newVal.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.') // 保证.只出现一次，而不能出现两次以上
  newVal = newVal.replace(partten, '$1$2.$3') // 只能输入precision个小数
  return newVal
}

// 获取横版更改标准值计算后的上下差值
export const getHorizonChangeValue = (row, tableList) => {
  const upDiffValueIndex = getDiffValueIndex('UpDiffValue', tableList, row)
  const downDiffValueIndex = getDiffValueIndex('DownDiffValue', tableList, row)
  if (row.attrCode != 'StandardValue') return // 非标准值的不需要计算逻辑
  if (row.checkItemType == CHECK_ITEM_TYPES.Number) {
    row.attrValueName = getRegReplaceVal(row.attrValueName)
  }
  const curVal = row.attrValueName
  // 件装数量、第二件数量、数量的验收事项 - 如果上下差值为空，给默认0
  if (CHECK_UUID_TYPES.includes(row.checkItemUuid) && !isFalsityExcludeZero(curVal)) {
    return {
      upDiffValueIndex,
      downDiffValueIndex,
      upDiffValue: tableList[upDiffValueIndex]?.attrValueName ?? 0,
      downDiffValue: tableList[downDiffValueIndex]?.attrValueName ?? 0,
    }
  }
  let upDiffValue, downDiffValue
  if (!isFalsityExcludeZero(curVal) && _.isFinite(Number(curVal))) {
    upDiffValue = getComputeDiffValue(curVal)
    downDiffValue = getComputeDiffValue(curVal)
  }
  return {
    upDiffValueIndex,
    downDiffValueIndex,
    upDiffValue,
    downDiffValue,
  }
}

// 获取竖版组装表列的key
export const getVerticalNewColKey = ({
  col, row, index, key, checkItemUuid, checkItemType
}) => `${col?.key ?? key}_${index}_${row?.checkItemUuid ?? checkItemUuid}_${row?.checkItemType ?? checkItemType}`

// 获取竖版更改标准值计算后的上下差值
export const getVerticalChangeValue = ({ row, val, columnName }) => {
  const [key, index, checkItemUuid, checkItemType] = columnName.split('_') ?? []
  const upDiffKey = getVerticalNewColKey({ key: 'upDiffValue', index, checkItemUuid, checkItemType })
  const downDiffKey = getVerticalNewColKey({ key: 'downDiffValue', index, checkItemUuid, checkItemType })
  if (!_.startsWith(columnName, key)) return
  if (checkItemType == CHECK_ITEM_TYPES.Text) return // 文本类型不需要计算逻辑 checkItemType：1-数字，2-文本
  row[columnName] = getRegReplaceVal(val)
  const curVal = row[columnName]
  // 件装数量、第二件数量、数量的验收事项 - 如果上下差值为空，给默认0
  if (CHECK_UUID_TYPES.includes(checkItemUuid) && !isFalsityExcludeZero(curVal)) {
    return {
      upDiffKey,
      downDiffKey,
      upDiffValue: row[upDiffKey] ?? 0,
      downDiffValue: row[downDiffKey] ?? 0,
    }
  }
  let upDiffValue, downDiffValue
  if (!isFalsityExcludeZero(curVal) && _.isFinite(Number(curVal))) {
    upDiffValue = getComputeDiffValue(curVal)
    downDiffValue = getComputeDiffValue(curVal)
  }
  return {
    upDiffKey,
    downDiffKey,
    upDiffValue,
    downDiffValue
  }
}
